#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :huaban_util.py
# @Time      :2023/7/27 
# @Author    :CL
# @email     :1037654919@qq.com

import requests
import time
import random
from utils import MongoDBUtil,get_html,mongo_manager
import math

MongoDBUtil = MongoDBUtil()
huaban_ =mongo_manager("huaban_",db="car_iamges")

def get_proxy():
    port = random.randint(24000, 24400)
    proxies = {'http': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}',
               'https': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}'}
    return proxies

class Huaban():
    def __init__(self):
        # 默认的域名
        self.host = "https://huaban.com/search?q=car"
        # 根据关键字key获取boards
        self.boards_url = "https://api.huaban.com/search/boards"
        # 根据rid获取歌曲下载链接的json数据的接口
        self.pins_url = "https://api.huaban.com/boards/{}/pins"

        # 一些必要 的请求头
        self.headers = {
            "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate, br",
            "Referer": "https://huaban.com/search?type=board&q=%E8%BD%A6",
            "Origin": "https://huaban.com",
            "Connection": "keep-alive",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-site",
            "Pragma": "no-cache",
            "Cache-Control": "no-cache"
        }
        self.cookies = {
            "user_device_id": "81a9c3e41f8b4dceb6ea3c987964d77c",
            "user_device_id_timestamp": "1690265509043",
            "Hm_lvt_d4a0e7c3cd16eb58a65472f40e7ee543": "1690265510",
            "Hm_lpvt_d4a0e7c3cd16eb58a65472f40e7ee543": "1690271438",
            "Hm_up_d4a0e7c3cd16eb58a65472f40e7ee543": "%7B%22version%22%3A%7B%22value%22%3A%222.0.0%22%2C%22scope%22%3A1%7D%2C%22has_plugin%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%7D",
            "_ga_50RYEM7F09": "GS1.1.1690338064.3.1.1690338076.0.0.0",
            "_ga": "GA1.2.705138313.1690265516",
            "_gid": "GA1.2.391768333.1690265544",
            "sid": "s%3Av9kh7OyMn1HpsN1h6Eb2C0YP59DR_SAa.JfBf3LsTyiVzetl%2BjZnf1TEryrnH7PsRqDzXyZBihEw",
            "newbietask": "1",
            "registered": "registered",
            "uid": "37215891",
            "gd_id": "2024933926397944859",
            "acw_tc": "0b32824216903374873624237ea17f6d71f4a8875c31ff58fcf5625d04c6f2",
            "_gat_UA-135559536-2": "1"
        }

    def my_get(self, url, stream=False):
        """自定义的请求函数，可以打印一些信息，和处理反爬"""
        proxies = get_proxy() #代理
        for __ in range(5):
            try:
                response = requests.get(url, headers=self.headers, proxies=proxies, stream=stream)
                if response.status_code == 200:
                    return response.json()
                time.sleep(5)
            except:
                pass
        return None

    def get_board_id(self,keyword='car', page=1):

        url = "https://api.huaban.com/search/boards"
        params = {
            "q": keyword,
            "sort": "all2",
            "page": page,
            "per_page": "20",
            "hide_other_count": "1",
            "fields": "boards:BOARD,facets,board_count"
        }
        response = requests.get(url, headers=self.headers, cookies=self.cookies, params=params)
        print(response)
        if response.status_code == 200:
            return response.json()
        else:
            return  None

    def get_pins(self,board_id='32067527',maxs =None):

        headers = {
            "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate, br",
            "Referer": "https://huaban.com//boards/63835150",
            "Origin": "https://huaban.com",
            "Connection": "keep-alive",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-site",
            "Pragma": "no-cache",
            "Cache-Control": "no-cache"
        }
        cookies = {
            "user_device_id": "81a9c3e41f8b4dceb6ea3c987964d77c",
            "user_device_id_timestamp": "1690265509043",
            "Hm_lvt_d4a0e7c3cd16eb58a65472f40e7ee543": "1690265510",
            "Hm_lpvt_d4a0e7c3cd16eb58a65472f40e7ee543": "1690267737",
            "Hm_up_d4a0e7c3cd16eb58a65472f40e7ee543": "%7B%22version%22%3A%7B%22value%22%3A%222.0.0%22%2C%22scope%22%3A1%7D%2C%22has_plugin%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%7D",
            # "_ga_50RYEM7F09": "GS1.1.1690265516.1.1.1690267735.0.0.0",
            "_ga": "GA1.2.705138313.1690265516",
            "_gid": "GA1.2.391768333.1690265544",
            "sid": "s%3Av9kh7OyMn1HpsN1h6Eb2C0YP59DR_SAa.JfBf3LsTyiVzetl%2BjZnf1TEryrnH7PsRqDzXyZBihEw",
            "newbietask": "1",
            "registered": "registered",
            "uid": "37215891",
            "gd_id": "2024933926397944859",
            # "acw_tc": "0b63bb2e16902675431892587e87197706ae479426696bb6e2e27bb13da8a1",
            "_gat_UA-135559536-2": "1"
        }
        params = {
            "limit": "40",
            "max": maxs,
            "fields": "pins:PIN,board:BOARD_DETAIL,check"
        }
        url = self.pins_url.format(board_id)
        response = requests.get(url, headers=headers, cookies=cookies, params=params)

        # print(response.text)
        print(response)
        if response.status_code == 200:
            return response.json()


if __name__ == "__main__":
    huaban = Huaban()
    #test
    keyword = 'car'
    page = 1
    res = huaban.get_board_id(keyword,page)
    print(res)
    # from pprint import pprint
    # data = huaban.get_pins(board_id='32067527')
    # pprint(data)
    # pass

